package org.jeecg.modules.wms.goods.service.impl;

import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.wms.goods.entity.WmsCargoOwners;
import org.jeecg.modules.wms.goods.mapper.WmsCargoOwnersMapper;
import org.jeecg.modules.wms.goods.service.IWmsCargoOwnersService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 货主表
 * @Author: jeecg-boot
 * @Date:   2025-09-02
 * @Version: V1.0
 */
@Service
public class WmsCargoOwnersServiceImpl extends ServiceImpl<WmsCargoOwnersMapper, WmsCargoOwners> implements IWmsCargoOwnersService {

    private final RedisUtil redisUtil;

    public WmsCargoOwnersServiceImpl(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Override
    public void add(WmsCargoOwners wmsCargoOwners) {
        //生成货主编码
        String code = generateOwnerCode();
        wmsCargoOwners.setOwnerCode(code);
        save(wmsCargoOwners);
    }

    private String generateOwnerCode() {
        //编码规则：C+5位序号，序号使用redis自增序号实现
        //调用redis的incr函数
        long incr = 0;

        try {
            incr = redisUtil.incr("WMS_CARGO_OWNERS_CODE", 1);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("生成货主编码出错");
        }
        String code = "C" +String.format("%05d", incr);
        return code;
    }
}
